let express = require('express')
let favorites = express.Router();
let {pool} = require('../db/pool');
favorites.get('/favorites/findAll',(req,res)=>{
    pool.getConnection((err,connection)=>{
        let sql = `select user_id from base_user where user_name='${req.query.username}'`
        connection.query(sql,(err,result)=>{
            let sql = `select * from favorites where collect_user='${result[0].user_id}' order by collect_time desc`
            connection.query(sql,(err,result)=>{
            res.send(result)
        })
        })
        connection.release();
        
    })
})
favorites.get('/favorites/toAddOne',(req,res)=>{
    pool.getConnection((err,connection)=>{
        let sql = `select user_id from base_user where user_name='${req.query.username}'`
        connection.query(sql,(err,result)=>{
            let id = Math.random()*1000000000;
            let time = Date.now();
            let sql = `insert into favorites (collect_id,collect_user,collect_comment,collect_time)
             values ('${id}','${result[0].user_id}','${req.query.movieComment}','${time}')`
            connection.query(sql,(err,result)=>{
                res.send(result)
            })
            connection.release();
        })
    })    
})
module.exports = {
    favorites
}